class ListNode{
    int val;
    ListNode next;
    ListNode prev;
    ListNode(int val){
        this.val=val;
    }
        }



public class MyLinkedList {
    public  static ListNode head;
    public  static ListNode last;
    public void display(){
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public int size(){
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    public boolean contain(int key){
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    public void addFirst(int data){
        ListNode  node=new ListNode(data);
        if(head==null) {
            head= node;
            last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }
    public void addLast(int data){
        ListNode  node=new ListNode(data);

        if(head==null) {
            head=node;
            last=node;
        }else{
            last.next=node;
            node.prev=last;
            last=node;
        }
    }
    public void remove(int key){
        ListNode cur=head;
        if(cur==null) return ;
        while (cur!=null){
            if(cur.val==key){
                if(head.val==key){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }else {
                        last=null;
                    }
                    return;
                }
                if(last.val==key){
                    last.prev.next=null;
                    last=last.prev;
                    return;
                }
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
                return;
            }
            cur=cur.next;
        }
    }
    public void removeAll(int key){
        ListNode cur=head;
        if(cur==null) return ;
        while (cur!=null){
            if(cur.val==key){
                if(head.val==key){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }else {
                        last=null;
                    }

                }
                else if(last.val==key){
                    last.prev.next=null;
                    last=last.prev;
                }else {
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }


            }
            cur=cur.next;
        }
    }

public void insertIndex(int index,int data){
     ListNode node=new ListNode(data);
     if(index<0||index>size()){
         System.out.println("位置不合法");
         return;
     }
     if(index==0){
         addFirst(data);
         return;
     } else if(index==size()){
         addLast(data);
     }else {
         ListNode cur=head;
         while (index!=0){
             cur=cur.next;
             --index;
         }
         node.next=cur;
         cur.prev.next=node;
         node.prev=cur.prev;
         cur.prev=node;
     }
}
public void clear(){
        ListNode cur=head;

        while (cur!=null){
            ListNode curNext=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curNext;
        }
        head=null;
        last=null;
}
}



